<?php

	namespace Phptricks\system\library\request_variables;

	/**
	 * Description of http_request_variables
	 *
	 * @author Sammy Guergachi <sguergachi at gmail.com>
	 */
	use Phptricks\system\core\library\library;
	use Phptricks\system\library\PhptricksExceptions\PhptricksExceptions as Exc;

	class request_variables implements library{

		public $_error = array();
		public $_server = array();
		public $_get = array();
		public $_post = array();
		public $_files = array();
		public $_request = array();
		private static $_instance = null;

		private function __construct(){
			if (isset($_FILES)) {
				$this->_files = &$_FILES;
			}
			if (isset($_SERVER)) {
				$this->_server = &$_SERVER;
			}
			if (isset($_GET)) {
				$this->_get = &$_GET;
			}

			if (isset($_POST)) {
				$this->_post = &$_POST;
			}
			if (isset($_REQUEST)) {
				$this->_request = &$_REQUEST;
			}
		}

		public static function &getinstance(){
			if (self::$_instance === null) {
				self::$_instance = new \Phptricks\system\library\request_variables\request_variables();
			}
			return self::$_instance;
		}

		public function _initialize(){
			
		}

		public function is_set($type, $varname){
			if (!isset($type) || empty($type)) {
				throw new Exc('you mast br set $type');
			}
			if (!isset($varname) || empty($varname)) {
				throw new Exc('you mast br set $varname');
			}
			switch ($type) {
				case 'server':
					if (isset($this->_server[$varname])) {
						return true;
					} else {
						return false;
					}
					break;
				case 'post':
					if (isset($this->_post[$varname])) {
						return true;
					} else {
						return false;
					}
					break;

				case 'get':
					if (isset($this->_get[$varname])) {
						return true;
					} else {
						return false;
					}
					break;

				case 'request':
					if (isset($this->_request[$varname])) {
						return true;
					} else {
						return false;
					}
					break;
				default:
					$this->_error[] = 'the $type mast be server | post | get | request not : ' . $type;
					return false;
					break;
			}
		}

		/**
		 * @param String $type server | get | post | request
		 * @param array $array_vars 
		 */
		public function setvar($type, $array_vars = array()){
			if (!isset($type) || empty($type)) {
				throw new Exc('you mast br set $type');
			}
			if (!isset($array_vars) && \is_array($array_vars)) {
				throw new Exc('you mast br set $array_vars');
			}

			if (count($array_vars) == 0) {
				throw new Exc('you mast br set $array_vars');
			}

			switch ($type) {
				case 'server':
					foreach ($array_vars as $key => $val) {
						$this->_server[$key] = $val;
					}
					break;
				case 'post':
					foreach ($array_vars as $key => $val) {
						$this->_post[$key] = $val;
					}
					break;

				case 'get':
					foreach ($array_vars as $key => $val) {
						$this->_get[$key] = $val;
					}
					break;

				case 'request':
					foreach ($array_vars as $key => $val) {
						$this->_request[$key] = $val;
					}
					break;
				default:
					$this->_error[] = 'the $type mast be server | post | get | request not : ' . $type;
					return false;
					break;
			}
		}

	}
	